﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisionModels;
using thinger.VisitionDAL;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;


namespace thinger.VisionDemo
{
    public enum dabiaoStep1
    {
        导入版本,
        发送变量,
        发送坐标,
        开始打标,
        打标完成
    }

    public enum plcStep1
    {
        请求放料,
        放料,
        放料完成,
        检测A,
        检测B,
        打码A,
        打码B,
        扫码A,
        扫码B,
        下料,
        下料完成,
        流程完成
    }

    public enum robot_shang_Step1
    {
        发送拍照位,
        抓取定位纠偏,
        放料,
        放料完成
    }

    public enum robot_xia_Step1
    {
        取料点,
        进入下料,
        下料完成
    }

    public partial class FrmDiy : Form
    {
        //---- 连接机器人
        public int robot_quliao_count = 1;//取料计次
        public int robot_okliao_count = 1;//ok料计次
        public int robot_ngliao_count = 1;//ng料计次
        public robot_shang_Step1 robot_quliao_Step = 0;
        public robot_xia_Step1 robot_xialiao_Step = 0;
        public int go_step_type = 0;//0-上料  1-ok料  2-NG料
        public int go_type = 1;//1-搬运1  2-搬运2
        public bool isRobotRun = false;//- 是否机械手在运行
        private object _lockObject = new object();//线程锁
        #region 连接机器人

        #endregion


        //---- 机器人发送命令
        #region 机器人发送命令
        private void robot_send_cmd(string str)
        {
   
        }
        #endregion

        //---- 机器人取料
        #region 机器人取料
        private void robot_quliao(string str)
        {
            AddLog(0, "机器人-正在抓取:" + robot_quliao_count.ToString());

            switch (robot_quliao_Step)
            {
                case robot_shang_Step1.发送拍照位:
                    if (str == "paizhaowei")
                    {
                        AddLog(0, "机器人-发送拍照位示教");
                        //发送点阵位置
                        string tempStr = GlobalVar.Instance.QUDatas[GlobalVar.Instance.CurrNumT].ToString();
                        robot_send_cmd(tempStr);
                        robot_quliao_Step++;
                    }
                    break;
                case robot_shang_Step1.抓取定位纠偏:
                    if (str == "ccd")
                    {
                        AddLog(0, "机器人-抓取定位纠偏");
                        //开始
                        //纠偏定位

                        //发送坐标

                        robot_quliao_Step++;
                    }
                    break;
                case robot_shang_Step1.放料:
                    if (str == "10ok")
                    {
                        AddLog(0, "机器人-放料");
                        if (go_type == 1)
                        {
                            robot_send_cmd("11");
                            robot_quliao_Step++;

                        }
                        else
                        {
                            robot_send_cmd("12");
                            currplc_step1++;
                            robot_quliao_Step++;
                        }
                    }
                    break;
                case robot_shang_Step1.放料完成:
                    if (str == "10ok")
                    {
                        AddLog(0, "机器人-放料");
                        if (go_type == 1)
                        {
                            var result = h5u.WriteH5U(4012, "11");//放料完成给信号
                            if (!result.IsSuccess)
                            {
                                AddLog(1, "搬运1-放料完成信号!");
                                robot_quliao_Step++;
                                GlobalVar.Instance.CurrNumT++;
                            }
                           

                        }
                        else
                        {
                            var result = h5u.WriteH5U(4052, "11");//放料完成给信号
                            if (!result.IsSuccess)
                            {
                                AddLog(1, "搬运2-放料完成信号!");
                                robot_quliao_Step++;
                                GlobalVar.Instance.CurrNumT++;
                            }
                        }
                    }
                    break;

            }

            if (GlobalVar.Instance.CurrNumT > GlobalVar.Instance.CurrNumTMax)
            {
                AddLog(0, "取料盘→OK盘");
                robot_quliao_count = 1;
                robot_okliao_count = 1;
                //robot_send_cmd("1");
            }
            Thread.Sleep(2000);


        }
        #endregion

        #region 机器人下料
        private void robot_xialiao(string str)
        {
            AddLog(0, "机器人-正在抓取:" + robot_okliao_count.ToString());
            switch (robot_xialiao_Step)
            {
                case robot_xia_Step1.取料点:

                    if (str == "13ok")
                    {
                        AddLog(0, "机器人-进入父程序");
                        robot_send_cmd("15");
                        robot_xialiao_Step++;

                    }
                    if (str == "14ok")
                    {
                        AddLog(0, "机器人-进入父程序");
                        robot_send_cmd("15");
                        robot_xialiao_Step++;

                    }
                    break;
                case robot_xia_Step1.进入下料:
                    if (str == "fangliao")
                    {
                        AddLog(0, "机器人-进入下料");
                        robot_send_cmd("15" + robot_okliao_count.ToString());
                        robot_xialiao_Step++;
                        //判断搬运 下料类型   发送下料坐标
                    }
                    break;
                case robot_xia_Step1.下料完成:
                    if (str == "15ok")
                    {
                        if (go_type == 1)
                        {
                            var result = h5u.WriteH5U(4016, "11");//下料1完成给信号
                            if (result.IsSuccess)
                            {
                                AddLog(1, "搬运1-下料完成!");
                                robot_xialiao_Step++;
                            }
                        }
                        else
                        {
                            var result = h5u.WriteH5U(4056, "11");//下料2完成给信号
                            if (result.IsSuccess)
                            {
                                AddLog(1, "搬运2-下料完成!");
                                robot_xialiao_Step++;
                            }
                        }
                    }
                    break;
            }
            if (GlobalVar.Instance.CurrNumNG > GlobalVar.Instance.CurrNumNGMax-1)
            {
                AddLog(0, "NG料满盘");
                //异常处理
                //robot_send_cmd("1");
                GlobalVar.Instance.CurrNumNG = 0;
            }
            if (GlobalVar.Instance.CurrNumOK > GlobalVar.Instance.CurrNumOKMax - 1)
            {
                AddLog(0, "OK料满盘");
                //异常处理
                //robot_send_cmd("1");
                GlobalVar.Instance.CurrNumOK = 0;
            }
            Thread.Sleep(2000);

        }
        #endregion





        //---- plc
        private H5U h5u = new H5U();
        public plcStep1 currplc_step1 = 0;
        public plcStep1 currplc_step2 = 0;
        //---- 连接PLC
        private void plc_lianjie()
        {
            if (this.button_plc_00.Text == "建立连接")
            {
                var result = h5u.OpenH5U("192.168.1.11");

                if (result.IsSuccess)
                {

                    AddLog(0, "连接成功");
                    this.button_plc_00.Text = "断开连接";
                }
                else
                {
                    AddLog(0, "连接失败");
                }
            }
            else
            {
                var result = h5u.CloseH5U();

                if (result.IsSuccess)
                {
                    AddLog(0, "断开连接成功");
                    this.button_plc_00.Text = "建立连接";
                }
                else
                {
                    AddLog(0, "断开连接失败");
                }
            }
        }
        //---- 初始化plc
        #region plc初始化
        private void plc_init()
        {
            var result = h5u.ReadH5U(4092);

            if (result.Message == "11")
            {

                AddLog(0, "收到plc信号 .....初始化");
                //机械手回原

                //给PLC信号 允许PLC初始化
                result = h5u.WriteH5U(4094, "11");//放料完成给信号
                //等待plc 约1分钟 完成初始化

                if (result.IsSuccess)
                {
                    AddLog(0, "等待plc 约1分钟 完成初始化");
                }

            }
            result = h5u.ReadH5U(4096);

            if (result.Message == "11")
            {
                AddLog(0, "plc初始化完成可以设置自动模式");

            }
        }
        #endregion
        //---- 放料1
        #region 放料1
        private void plc_fangliao_1()
        {
            //plc允许放料
            var result = h5u.ReadH5U(4010);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-放料");
                return;
            }
            else if (currplc_step1 == plcStep1.请求放料)
            {
                robot_send_cmd("10");
                currplc_step1++;
                return;
            }
            // 机器手放料完成

            //
            result = h5u.WriteH5U(4012, "11");//放料完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-不放料!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-放料完成!");
            }

        }
        #endregion
        //---- 检测A1
        #region 检测A1
        private void plc_jiance_A1()
        {

            //plc A面检测请求
            var result = h5u.ReadH5U(4020);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-A面检测!");
                return;
            }

            // 检测处理

            //检测A面完成给信号
            result = h5u.WriteH5U(4024, "11");//检测A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-A面不检测!");
            }
            else
            {

                currplc_step1++;
                AddLog(0, "搬运1-A面检测完成!");
            }
        }
        #endregion
        //---- 检测B1
        #region 检测B1
        private void plc_jiance_B1()
        {

            //plc B面扫码请求
            var result = h5u.ReadH5U(4022);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-B面检测!");
                return;
            }

            // 扫码处理

            //扫码B面完成给信号
            result = h5u.WriteH5U(4026, "11");//扫码B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-B面不检测!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-B面检测完成!");
            }

        }
        #endregion
        //---- 打码A1
        #region 打码A1
        private void plc_dama_A1()
        {

            //plc A面打码请求
            var result = h5u.ReadH5U(4030);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-A面打码");
                return;
            }

            // 打码处理

            //打码A面完成给信号
            result = h5u.WriteH5U(4034, "11");//打码A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-A面不打码!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-A面打码完成!");
            }
        }
        #endregion

        //---- 打码B1
        #region 打码B1
        private void plc_dama_B1()
        {

            //plc B面打码请求
            var result = h5u.ReadH5U(4032);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-B面打码");
                return;
            }

            // 打码处理

            //打码B面完成给信号
            result = h5u.WriteH5U(4036, "11");//打码B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-B面不打码!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-B面打码完成!");
            }
        }
        #endregion

        //---- 扫码A1
        #region 扫码A
        private void plc_saoma_A1()
        {

            //plc A面扫码请求
            var result = h5u.ReadH5U(4040);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-A面扫码");
                return;
            }

            // 扫码处理

            //扫码A面完成给信号
            result = h5u.WriteH5U(4044, "11");//扫码A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-A面不扫码!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-A面扫码完成!");
            }
        }
        #endregion
        //---- 扫码B1
        #region 扫码B1
        private void plc_saoma_B1()
        {

            //plc B面扫码请求
            var result = h5u.ReadH5U(4042);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-B面扫码");
                return;
            }

            // 扫码处理

            //扫码B面完成给信号
            result = h5u.WriteH5U(4046, "11");//扫码B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运1-B面不扫码!");
            }
            else
            {
                currplc_step1++;
                AddLog(0, "搬运1-B面扫码完成!");
            }

        }
        #endregion
        //---- 下料1
        #region 下料1
        private void plc_xialiao_1()
        {
            var result = h5u.ReadH5U(4014);
            if (result.Message != "11")
            {
                AddLog(1, "搬运1-下料");
                return;
            }
            else if ((currplc_step1 == plcStep1.下料))
            {

                robot_send_cmd("13");
                currplc_step1++;
                return;
            }

            // 下料1处理

            //下料1完成给信号
            //result = h5u.WriteH5U(4016, "11");//下料1完成给信号
            //if (!result.IsSuccess)
            //{
            //    AddLog(1, "搬运1-不下料!");
            //}
            //else if (currplc_step1 == plcStep1.下料完成)
            //{
            //    AddLog(0, "搬运1-下料完成!");
            //    currplc_step1++;
            //    return;
            //}
        }
        #endregion
        //---- 放料2
        #region 放料2
        private void plc_fangliao_2()
        {

            //plc允许放料
            var result = h5u.ReadH5U(4050);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-放料");
                return;
            }
            else if (currplc_step2 == plcStep1.请求放料)
            {
                robot_send_cmd("10");
                currplc_step2++;
                return;
            }

            // 机器手放料完成

            //
            result = h5u.WriteH5U(4052, "11");//放料完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-不放料!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-放料完成!");
            }
        }
        #endregion
        //---- 检测A2
        #region 检测A2
        private void plc_jiance_A2()
        {

            //plc A面检测请求
            var result = h5u.ReadH5U(4060);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-A面检测");
                return;
            }

            // 检测处理

            //检测A面完成给信号
            result = h5u.WriteH5U(4064, "11");//检测A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-A面不检测!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-A面检测完成!");
            }
        }
        #endregion

        //---- 检测B2
        #region 检测B2
        private void plc_jiance_B2()
        {

            //plc B面检测请求
            var result = h5u.ReadH5U(4062);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-B面检测");
                return;
            }

            // 检测处理

            //检测B面完成给信号
            result = h5u.WriteH5U(4066, "11");//检测B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-B面不检测!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-B面检测完成!");
            }
        }
        #endregion
        //---- 打码A2
        #region 打码A2
        private void plc_dama_A2()
        {

            //plc A面打码请求
            var result = h5u.ReadH5U(4070);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-A面打码");
                return;
            }

            // 打码处理

            //打码A面完成给信号
            result = h5u.WriteH5U(4074, "11");//打码A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-A面不打码!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-A面打码完成!");
            }
        }
        #endregion

        //---- 打码B2
        #region 打码B
        private void plc_dama_B2()
        {

            //plc B面打码请求
            var result = h5u.ReadH5U(4072);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-B面打码");
                return;
            }

            // 打码处理

            //打码B面完成给信号
            result = h5u.WriteH5U(4076, "11");//打码B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-B面不打码!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-B面打码完成!");
            }
        }
        #endregion
        //---- 扫码A2
        #region 扫码A2
        private void plc_saoma_A2()
        {

            //plc A面扫码请求
            var result = h5u.ReadH5U(4080);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-A面扫码");
                return;
            }

            // 扫码处理

            //扫码A面完成给信号
            result = h5u.WriteH5U(4084, "11");//扫码A面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-A面不扫码!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-A面扫码完成!");
            }
        }
        #endregion
        //---- 扫码B2
        #region 扫码B2
        private void plc_saoma_B2()
        {

            //plc B面扫码请求
            var result = h5u.ReadH5U(4082);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-B面扫码");
                return;
            }

            // 扫码处理

            //扫码B面完成给信号
            result = h5u.WriteH5U(4086, "11");//扫码B面完成给信号
            if (!result.IsSuccess)
            {
                AddLog(1, "搬运2-B面不扫码!");
            }
            else
            {
                currplc_step2++;
                AddLog(0, "搬运2-B面扫码完成!");
            }
        }
        #endregion
        //---- 下料2
        #region 下料2
        private void plc_xialiao_2()
        {
            //plc 下料2
            var result = h5u.ReadH5U(4054);
            if (result.Message != "11")
            {
                AddLog(1, "搬运2-下料!");
                return;
            }
            else if (currplc_step2 == plcStep1.下料)
            {
                robot_send_cmd("14");
                currplc_step2++;
                return;
            }
            // 下料2处理

            //下料2完成给信号
            //result = h5u.WriteH5U(4056, "11");//下料2完成给信号
            //if (!result.IsSuccess)
            //{
            //    AddLog(1, "搬运2-不下料!");
            //}
            //else if (currplc_step2 == plcStep1.下料完成)
            //{
            //    currplc_step2++;
            //    AddLog(0, "搬运2-下料成功!");
            //}
        }
        #endregion




        //打标
        #region 打标


        #endregion

        //扫码
        #region 扫码

        #endregion


        //--new 机器人
        private Socket socketServer;
        //创建集合字典，键是clientIP 值是socketServer
        private Dictionary<string, Socket> CurrentClienlist = new Dictionary<string, Socket>();

        //-- 扫码

        private Socket socketSaoma;
        // -- old
        static string renwu_name;//任务名
        static string renwu_index;//任务序号
        public FrmDiy()
        {
            InitializeComponent();
            this.FormClosing += FrmCalibration_FormClosing;
        }

        private void FrmCalibration_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        //-------- new
        /// <summary>
        /// 监听线程
        /// </summary>
        private Thread th_listen;
        /// <summary>
        /// 监听Socket
        /// </summary>
        private Socket listenSkt;
        /// <summary>
        /// 用于通讯的Socket
        /// </summary>
        internal Socket commSkt;
        /// <summary>
        /// 用于存放已连接的socket
        /// </summary>
        Dictionary<string, Socket> L_connectedSocket = new Dictionary<string, Socket>();
        /// <summary>
        /// 窗体实例对象
        /// </summary>
        /// 

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg">要发送的信息</param>
        /// <param name="sender">消息发送者</param>
        internal void Send(string msg, string sender)
        {
            try
            {
                byte[] buffer = Encoding.Default.GetBytes(msg);
                L_connectedSocket[sender].Send(buffer);
                AddLog(0, "-> 机器人：" + msg);
            }
            catch (Exception ex)
            {
                AddLog(1, "-> 机器人：发送失败" + ex.Message);
            }
        }


        /// <summary>
        /// 消息接收
        /// </summary>
        /// <param name="obj">通讯用的Socket</param>
        private void Recieve(object obj)
        {
            try
            {
                Socket socket = obj as Socket;

                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    int i = socket.Receive(buffer);

                    //接收到的数据长度为0时表示连接断开，跳出循环
                    if (i == 0)
                    {
                        break;
                    }
                    string str = Encoding.UTF8.GetString(buffer, 0, i);
                    //获取客户端的端口号
                    AddLog(0, "收到数据:" + str);
                    string str2 = socket.RemoteEndPoint.ToString();
                    AddLog(0, "ip地址: " + str2);

                    if (str is "3000")
                    {

                    }
                    switch (renwu_name)
                    {
                        case "上料":
                            go_shangliao(str);
                            break;
                        case "ok料":
                            go_okliao(str);
                            break;
                        case "ng料":
                            go_ngliao(str);
                            break;
                        default:
                            break;
                    }


                }
            }
            catch (Exception ex)
            {
                //LogHelper.SaveErrorInfo(ex);
                AddLog(0, "->HOME ERROR:" + ex.Message);
                return;
            }
        }

        private void go_shangliao(string str)
        {
            switch (str)
            {
                case "10ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("101");
                    }
                    break;
                case "101ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("102");
                    }
                    break;
                case "102ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("103");
                    }
                    break;
                case "103ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("104");
                    }
                    break;
                case "104ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("105");
                    }
                    break;
                case "105ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("106");
                    }
                    break;
                case "106ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("107");
                    }
                    break;
                case "107ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("108");
                    }
                    break;
                case "108ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("10");
                    }
                    break;
                default:
                    break;
            }

        }

        private void go_okliao(string str)
        {
            switch (str)
            {
                case "10ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("101");
                    }
                    break;
                case "101ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("102");
                    }
                    break;
                case "102ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("103");
                    }
                    break;
                case "103ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("104");
                    }
                    break;
                case "104ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("105");
                    }
                    break;
                case "105ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("106");
                    }
                    break;
                case "106ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("107");
                    }
                    break;
                case "107ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("108");
                    }
                    break;
                case "108ok":
                    if (renwu_name == "10")
                    {
                        send_robot_cmd("10");
                    }
                    break;
                default:
                    break;
            }

        }

        private void go_ngliao(string str)
        {
            switch (str)
            {
                case "15ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("151");
                    }
                    break;
                case "151ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("152");
                    }
                    break;
                case "152ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("153");
                    }
                    break;
                case "153ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("154");
                    }
                    break;
                case "154ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("155");
                    }
                    break;
                case "155ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("156");
                    }
                    break;
                case "156ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("157");
                    }
                    break;
                case "157ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("158");
                    }
                    break;
                case "158ok":
                    if (renwu_name == "15")
                    {
                        send_robot_cmd("15");
                    }
                    break;
                default:
                    break;
            }

        }




        /// <summary>
        /// 监听
        /// </summary>
        /// <param name="obj">监听用的Socket</param>
        private void WaitConnect(object obj)
        {
            try
            {
                Socket listenSocket = obj as Socket;
                while (true)
                {
                    commSkt = listenSocket.Accept();

                    L_connectedSocket.Add(commSkt.RemoteEndPoint.ToString(), commSkt);
                    if (L_connectedSocket.Count() > 0)
                    {
                        AddLog(0, "有客户端接入");
                    }
                    //cbx_connectedMember.Items.Add(commSkt.RemoteEndPoint.ToString());
                    //lbx_connectedNumber.Items.Add(commSkt.RemoteEndPoint.ToString());
                    //if (cbx_connectedMember.Items.Count == 1)
                    //    cbx_connectedMember.SelectedIndex = 0;
                    Thread th = new Thread(Recieve);
                    th.IsBackground = true;
                    th.Start(commSkt);
                }
            }
            catch (Exception ex)
            {
                //LogHelper.SaveErrorInfo(ex);
            }
        }

        /// <summary>
        /// 保存IP和Port
        /// </summary>
        private void Save()
        {
            try
            {
                //Configuration.localIPAsSever   = tbx_ip.Text.Trim();
                //Configuration.localPortAsSever   = Convert.ToInt32(tbx_port.Text.Trim());
                //Configuration.Save();
            }
            catch (Exception ex)
            {
                //LogHelper.SaveErrorInfo(ex);
            }
        }
        /// <summary>
        /// 开始监听
        /// </summary>
        public void Listen()
        {
            try
            {
                if (btn_listen.Text == "开始监听")
                {
                    listenSkt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    listenSkt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    IPAddress ip = IPAddress.Parse("0.0.0.0");
                    IPEndPoint point = new IPEndPoint(ip, 3001);

                    try
                    {
                        listenSkt.Bind(point);
                    }
                    catch
                    {
                        this.TopMost = false;
                        //Frm_MessageBox.Instance.TopMost = true;
                        //Frm_MessageBox.Instance.MessageBoxShow("\r\n服务端在监听时出错！(错误代码：001)\r\n\r\n可能原因：所监听的IP地址有误");
                        this.TopMost = true;
                        return;
                    }
                    listenSkt.Listen(10);
                    th_listen = new Thread(WaitConnect);
                    th_listen.IsBackground = true;
                    th_listen.Start(listenSkt);
                    tbx_ip.Enabled = false;
                    tbx_port.Enabled = false;
                }
                else
                {
                    // 未开启监听
                    tbx_ip.Enabled = true;
                    tbx_port.Enabled = true;
                    btn_listen.Text = "开始监听";
                    th_listen.Suspend();
                    th_listen.Abort();
                    listenSkt.Dispose();
                    listenSkt.Close();
                }
            }
            catch (Exception ex)
            {
                //LogHelper.SaveErrorInfo(ex);
            }
        }




        ///---endnew





        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void goto_robot_cmd(string str_)
        {
            //if (str.IndexOf("error") != -1)
            //{
            //    robotCount = 0;
            //    if (str.IndexOf("gohome") != -1)
            //    {
            //        AddLog(1, "机器人回原异常");
            //        AddLog(0, "需手动回原");
            //        AddLog(0, "需手动回原");
            //    }
            //    if (str.IndexOf("zhenkong") != -1)
            //    {
            //        AddLog(1, "真空气缸异常");
            //        AddLog(0, "需手动回原");
            //        AddLog(0, "需手动回原");
            //    }

            //    if (str == "error")
            //    {
            //        AddLog(1, "程序调用异常");
            //    }



            //}
            //else
            //{
            //    switch (str)
            //    {
            //        case "0ok":
            //            robotCount = 0;
            //            break;
            //        case "1":

            //            break;
            //        case "2":
            //            break;
            //        case "3":
            //            break;
            //        case "4":
            //            break;
            //        case "10":

            //            break;
            //        case "11":
            //            break;
            //        default:
            //            break;
            //    }
            //}


        }

        public void robotStep()
        {




        }


        public void shangliaoStep()
        {




        }

        public void xialiaoStep()
        {




        }

        public void NGliaoStep()
        {




        }










        #region 添加日志
        /// <summary>
        /// 当前时间
        /// </summary>
        private string CurrentTime
        {
            get { return DateTime.Now.ToString("HH:mm:ss"); }
        }

        /// <summary>
        /// 添加日志信息
        /// </summary>
        /// <param name="index">日志等级</param>
        /// <param name="log">日志信息</param>
        public void AddLog(int index, string log)
        {
        
        }
        #endregion


        private void send_robot_cmd(string str)
        {
            try
            {
                byte[] buffer = Encoding.Default.GetBytes(str);
                L_connectedSocket["127.0.0.1:55279"].Send(buffer);
                AddLog(0, " ->机器人：" + str);
            }
            catch (Exception ex)
            {
                AddLog(1, " ->机器人 错误：" + ex.Message);
            }
        }

        private void btn_listen_Click(object sender, EventArgs e)
        {
            Listen();
        }

        private void button_ceshi_Click(object sender, EventArgs e)
        {

            renwu_name = "上料";
            renwu_index = "0";
            send_robot_cmd("10");
            //MessageBox.Show(L_connectedSocket.ToString());
            AddLog(0, " ->机器人：" + "开始上料任务");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            renwu_name = "ok料";
            renwu_index = "0";
            send_robot_cmd("15");
            //MessageBox.Show(L_connectedSocket.ToString());
            AddLog(0, " ->机器人：" + "开始ok料任务");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            renwu_name = "ng料";
            renwu_index = "0";
            send_robot_cmd("15");
            //MessageBox.Show(L_connectedSocket.ToString());
            AddLog(0, " ->机器人：" + "开始ng料任务");
        }

        private void headPanel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void headPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tbx_port_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void tbx_ip_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void headPanel6_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lst_Info_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_listen_Click_1(object sender, EventArgs e)
        {
            //robotInit();
            //socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //IPEndPoint iPe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse("4001"));
            //try
            //{
            //    socketServer.Bind(iPe);
            //}
            //catch (Exception ex)
            //{
            //    //失败写入日志
            //    AddLog(1, "机器人端口创建失败" + "：" + ex.Message);
            //}

            //socketServer.Listen(100);
            ////创建监听线程
            //Task.Run(new Action(() =>
            //{
            //    CheckedListen();
            //}));
        }

        //private void CheckedListen()
        //{
        //    while (true)
        //    {
        //        Socket socketClient = socketServer.Accept();
        //        string client = socketClient.RemoteEndPoint.ToString();
        //        CurrentClienlist.Add(client, socketClient);
        //        Task.Run(new Action(() =>
        //        {
        //            RecieveMessage(socketClient, client);
        //        }));
        //    }
        //}

        //private void RecieveMessage(Socket socketClien, string client)
        //{
        //    while (true)
        //    {
        //        //创建一个缓冲区
        //        byte[] buffer = new byte[1024 * 1024 * 2];
        //        int length = -1;
        //        //AddLog(0, client + "上线了");

        //        try
        //        {
        //            length = socketClien.Receive(buffer);
        //        }
        //        catch (Exception ex)
        //        {
        //            AddLog(1, client + "下线了");
        //            if (CurrentClienlist[client] != null)
        //            {
        //                CurrentClienlist.Remove(client);
        //            }

        //            break;
        //        }

        //        if (length > 0)
        //        {
        //            string msg = Encoding.Default.GetString(buffer, 0, length);

        //            AddLog(0, "收到" + client + "：" + msg);
        //        }
        //        else
        //        {
        //            //连接终止
        //            AddLog(1, client + "下线了");
        //            if (CurrentClienlist[client] != null)
        //            {
        //                CurrentClienlist.Remove(client);
        //            }

        //            break;
        //        }
        //    }
        //}

        private void button_ceshi_Click_1(object sender, EventArgs e)
        {
            AddLog(0, "机器人-返回原点");
            robot_send_cmd("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            socketSaoma = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint iPeee = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse("2001"));
            try
            {
                socketSaoma.Connect(iPeee);
            }
            catch (Exception ex)
            {
                //失败写入日志
                AddLog(0, "连接扫码失败：" + ex.Message);
                return;
            }
            AddLog(1, "连接扫码成功");
            //创建监听线程
            Task.Run(new Action(() =>
            {
                CheckedRecieveMessage();
            }));
        }

        private void CheckedRecieveMessage()
        {
            while (true)
            {
                //创建一个缓冲区
                byte[] buffer = new byte[1024 * 1024 * 1];
                int length = -1;
                try
                {
                    length = socketSaoma.Receive(buffer);
                }
                catch (Exception ex)
                {


                    //
                    AddLog(1, "下线了");
                }

                if (length > 0)
                {
                    string msg = Encoding.UTF8.GetString(buffer, 0, length); ;
                    AddLog(0, "收到" + socketSaoma.RemoteEndPoint.ToString() + "：" + msg);
                }
                else
                {
                    //连接终止
                    AddLog(1, "服务器断开连接！");
                    break;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (socketSaoma != null)
            {
                socketSaoma.Send(Encoding.Default.GetBytes("hhhh"));
            }

            //扫码关闭
            socketSaoma?.Close();

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }



        private void button5_Click_1(object sender, EventArgs e)
        {

            robot_send_cmd(tbx_port.Text);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            plc_init();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            robot_send_cmd("1");
            plc_fangliao_1();
            Task.Run(new Action(() =>
            {
                RunPlc1();
            }));

            Task.Run(new Action(() =>
            {
                RunPlc2();
            }));
        }

        private void RunPlc1()
        {

            while (true)
            {

                if (currplc_step1 == currplc_step2)
                {
                    Thread.Sleep(300);
                }
                else
                {
                    switch (currplc_step1)
                    {
                        case plcStep1.请求放料:
                            AddLog(0, "搬运1放料");
                            plc_fangliao_1();
                            break;
                        case plcStep1.放料:
                            currplc_step1++;
                            break;
                        case plcStep1.检测A:
                            AddLog(0, "搬运1检测A面");
                            plc_jiance_A1();
                            break;
                        case plcStep1.检测B:
                            AddLog(0, "搬运1检测B面");
                            plc_jiance_B1();
                            break;
                        case plcStep1.打码A:
                            AddLog(0, "搬运1打码A面");
                            plc_dama_A1();
                            break;
                        case plcStep1.打码B:
                            AddLog(0, "搬运1打码B面");
                            plc_dama_B1();
                            break;
                        case plcStep1.扫码A:
                            AddLog(0, "搬运1扫码A面");
                            plc_saoma_A1();
                            break;
                        case plcStep1.扫码B:
                            AddLog(0, "搬运1扫码B面");
                            plc_saoma_B1();
                            break;
                        case plcStep1.下料:
                            AddLog(0, "搬运1下料-下料...");
                            plc_xialiao_1();
                            break;
                        case plcStep1.下料完成:
                            AddLog(0, "搬运1下料完成");
                            // currplc_step1++;
                            break;
                        case plcStep1.流程完成:
                            AddLog(0, "搬运1-流程完成");
                            currplc_step1 = plcStep1.请求放料;
                            break;
                        default:
                            break;
                    }
                }

                Thread.Sleep(2000);
            }

        }



        private void RunPlc2()
        {
            while (true)
            {

                if (currplc_step1 == currplc_step2)
                {
                    Thread.Sleep(300);
                }
                else
                {
                    switch (currplc_step2)
                    {

                        case plcStep1.请求放料:
                            AddLog(0, "搬运2放料");
                            plc_fangliao_2();
                            break;

                        case plcStep1.放料:
                            AddLog(0, "搬运2放料");
                            currplc_step2++;
                            break;
                        case plcStep1.检测A:
                            AddLog(0, "搬运2检测A面");
                            plc_jiance_A2();
                            break;
                        case plcStep1.检测B:
                            AddLog(0, "搬运2检测B面");
                            plc_jiance_B2();
                            break;
                        case plcStep1.打码A:
                            AddLog(0, "搬运2打码A面");
                            plc_dama_A2();
                            break;
                        case plcStep1.打码B:
                            AddLog(0, "搬运2打码B面");
                            plc_dama_B2();
                            break;
                        case plcStep1.扫码A:
                            AddLog(0, "搬运2扫码A面");
                            plc_saoma_A2();
                            break;
                        case plcStep1.扫码B:
                            AddLog(0, "搬运2扫码B面");
                            plc_saoma_B2();
                            break;
                        case plcStep1.下料:
                            AddLog(0, "搬运2下料-下料...");
                            plc_xialiao_2();
                            break;
                        case plcStep1.下料完成:
                            AddLog(0, "搬运2下料完成");
                            //currplc_step2++;
                            break;
                        case plcStep1.流程完成:
                            AddLog(0, "搬运2-流程完成");
                            currplc_step2 = plcStep1.请求放料;
                            break;
                        default:
                            break;
                    }
                }

                Thread.Sleep(2000);
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            plc_lianjie();
        }

        private void button11_Click_1(object sender, EventArgs e)
        {

        }
    }
}
